import ContextMenu from "@imengyu/vue3-context-menu"
import { h } from 'vue'
import "../../contentMenu.scss"
import { ElMessageBox, ElMessage } from "element-plus";
import { userStore } from "../store/userStore"
import { sendAddFriend } from "./socketPageService"
// import { connectWebSocket } from "./webSocketService";
import { switchDetailStatus } from "../components/Menu/menu";

export const onContextMenu = (
    event: MouseEvent,
    websocket: WebSocket,
    friend: any,
    // menuType: 'chat' | 'add'
): void => {
    event.preventDefault();
    const store = userStore();
    try {
        // 初始化菜单项
        const menuItems: any[] = [
            {
                label: () => h('p', { class: 'menu-item-add' }, 'add'),
                onClick: () => {
                    showAddFriendPrompt(websocket, friend);
                }
            },
            {
                label: () => h('h', { class: 'menu-item-details' }, 'detail'),
                onClick: () => {
                    switchDetailStatus(friend);
                }
            }
        ];
        ContextMenu.showContextMenu({
            x: event.clientX,
            y: event.clientY,
            items: menuItems,
            zIndex: 9999,
            minWidth: 80
        })
    } catch (error) {
        console.error(error)
    }
}




// 添加好友的逻辑、提取到函数中方便后期管理
export const showAddFriendPrompt = (websocket: WebSocket, friend: any) => {
    const store = userStore();
    ElMessageBox.prompt('输入问候语(支持默认值、空值、自定义)', '好友申请', {
        confirmButtonText: 'sure',
        cancelButtonText: 'cancel',
        inputPlaceholder: "111",
        inputValue: `你好，我是：${store.userInfo.username}` as string
    }).then(({ value }) => {
        const addFriendChatInfo = {
            toChatId: friend.uu_id,
            content: value,
            friendAvatar: userStore().userInfo.avatar,
            friendName: userStore().userInfo.username,
            messageType: "add_friend_request"
        };
        sendAddFriend(websocket, addFriendChatInfo);
    }).catch((error) => {
        ElMessage.error(`信息发送失败:${error}`);
    });
}


